<template>
	<div class="hy-container">
		<HyPageHeader :title="`专家${title}`" :buttonData="buttonData">
			<!-- 审批流程，审核中流程通过和不通过，展示 -->
			<Schedule class="mb-5" v-if="scheduleAndHistoryShow && routerParams.processInstanceId" :processInstanceId="routerParams.processInstanceId" />
			<div v-if="['add', 'edit', 'submit'].includes(routerParams.operateType)">
				<BasicInfo ref="basicInfoRef" :detailData="basicData" />
				<div class="mt-5"></div>
				<EducationInfo ref="educationInfoRef" :detailData="educationData" />
				<div class="mt-5"></div>
				<CareerInfo ref="careerInfoRef" :detailData="careerData" />
				<div class="mt-5"></div>
				<SpecialityInfo ref="specialityInfoRef" :detailData="expertiseData" />
			</div>
			<div v-if="routerParams.operateType === 'view' || routerParams.operateType === 'approve'">
				<Detail :basicData="basicData" :careerData="careerData" :educationData="educationData" :expertiseData="expertiseData" />
			</div>
			<History class="mt-5" v-if="scheduleAndHistoryShow && routerParams.processInstanceId" :processInstanceId="routerParams.processInstanceId" />
		</HyPageHeader>
		<Start ref="startRef" @submitPass="submitPass"></Start>
		<ExamineBox ref="examineBoxRef" @submitPass="submitPass"></ExamineBox>
	</div>
</template>
<script setup lang="ts">
import HyPageHeader from '/@/components/HyPageHeader/index.vue';
import BasicInfo from '../components/BasicInfo.vue';
import CareerInfo from '../components/CareerInfo.vue';
import EducationInfo from '../components/EducationInfo.vue';
import SpecialityInfo from '../components/SpecialityInfo.vue';
import Detail from '../components/Detail.vue';
import { useRoute } from 'vue-router';
import { expertInfoAddApi, expertInfoSubmitApi } from '/@/api/expertDatabase/expert';
import { ElLoading, ElMessage } from 'element-plus';
import { useDetail } from '../hooks/useDetail';
import mittBus from '/@/utils/mitt';
import Start from '@cqdcg/admin/views/flow/form/tools/startFlow';
import ExamineBox from '@cqdcg/admin/views/flow/task/examineBox';
import Schedule from '@cqdcg/admin/views/flow/task/Schedule/index';
import History from '@cqdcg/admin/views/flow/task/History/index';
import { flowName } from '/@/constants';
import { useOperate } from '../hooks/useOperate';

const { title, basicData, careerData, educationData, expertiseData, routerParams, getDetailData } = useDetail();
const route = useRoute();
const basicInfoRef = ref();
const educationInfoRef = ref();
const careerInfoRef = ref();
const specialityInfoRef = ref();
//获取提交参数
const getParams = () => {
	const basicData = basicInfoRef.value?.getData();
	const educationData = educationInfoRef.value?.getData();
	const careerData = careerInfoRef.value?.getData();
	const specialityData = specialityInfoRef.value?.getData();
	const params = {
		...basicData,
		...educationData,
		...careerData,
		...specialityData,
		blacklist: '0',
	};
	return params;
};
// 定义一个函数来执行检查和报告错误
const checkAndReportError = async (checkFunction: any, errorMessage: string) => {
	try {
		return await checkFunction();
	} catch (error) {
		ElMessage.error(errorMessage);
		return false;
	}
};
const handleSubmit = async (type: number) => {
	if (type === 1) {
		// 执行检查和报告错误的逻辑
		const basicResult = await checkAndReportError(basicInfoRef.value.check, '基本信息填写错误');
		if (!basicResult) return;
		const educationResult = await checkAndReportError(educationInfoRef.value.check, '基本信息填写填写错误');
		if (!educationResult) return;
		const careerResult = await checkAndReportError(careerInfoRef.value.check, '职业信息填写填写错误');
		if (!careerResult) return;
		const specialityResult = await checkAndReportError(specialityInfoRef.value.check, '专业信息填写填写错误');
		if (!specialityResult) return;
	}
	const params = getParams();
	const loading = ElLoading.service({
		lock: true,
		text: '提交中...',
	});
	let request: any;
	if (type === 0) {
		request = expertInfoAddApi;
	}
	if (type === 1) {
		request = expertInfoSubmitApi;
	}
	request(params)
		.then((res: any) => {
			//新增，待提交，不跳转
			if (type === 0) {
				getDetailData(res.expertInfoId);
				ElMessage.success('保存成功');
			} else if (type === 1) {
				startRef.value.handle({
					flowName: flowName.EXPERT,
					businessKey: res.expertInfoId,
				});
			}
		})
		.finally(() => {
			loading.close();
		});
};
//审核
const examineBoxRef = ref();
const handleApprove = () => {
	examineBoxRef.value.handle({
		nodeId: routerParams.value.nodeId,
		taskId: routerParams.value.taskId,
		processInstanceId: routerParams.value.processInstanceId,
		paramMap: {
			businessKey: basicData.value.expertInfoId,
		},
	});
};
//设置按钮
const scheduleAndHistoryShow = ref(false);
const buttonData = computed(() => {
	//审核管理根据流程状态设置按钮
	if (routerParams.value.processStatus) {
		scheduleAndHistoryShow.value = true;
		switch (routerParams.value.processStatus) {
			case '1':
				return [{ name: '审核', class: 'blue-button', click: () => handleApprove() }];
			default:
				return [];
		}
	}
	//专家管理根据专家状态设置按钮
	else {
		const { deleteData } = useOperate(basicData.value, submitPass);
		switch (basicData.value.status) {
			case '1':
			case '4':
				if (basicData.value.status === '4') {
					scheduleAndHistoryShow.value = true;
				} else {
					scheduleAndHistoryShow.value = false;
				}
				//详情，点击保存提交，去编辑页面
				if (routerParams.value.operateType === 'view') {
					const setRouterParams = (operateType: string) => {
						const { routerGo } = useOperate({ ...routerParams.value, operateType });
						routerGo();
					};
					return [
						{ name: '编辑', class: 'white-button', click: () => setRouterParams('edit') },
						{ name: '提交', class: 'blue-button', click: () => setRouterParams('submit') },
						{ name: '删除', class: 'red-button', click: () => deleteData() },
					];
				} else {
					return [
						{ name: '保存', class: 'white-button', click: () => handleSubmit(0) },
						{ name: '提交', class: 'blue-button', click: () => handleSubmit(1) },
					];
				}
			//通过
			case '3':
				const { moveInBlackList, moveOutBlackList } = useOperate({ expertInfoId: routerParams.value.expertInfoId }, submitPass);
				scheduleAndHistoryShow.value = true;
				if (routerParams.value.blackList === '0') {
					return [{ name: '移入黑名单', click: () => moveInBlackList(), class: 'red-button' }];
				}
				if (routerParams.value.blackList === '1') {
					return [{ name: '移出黑名单', click: () => moveOutBlackList(), class: 'blue-button' }];
				} else {
					return [];
				}
			//审核中
			case '2':
				scheduleAndHistoryShow.value = true;
				return [];
			default:
				break;
		}
	}
});

const startRef = ref();
const submitPass = () => {
	mittBus.emit('onCurrentContextmenuClick', Object.assign({}, { contextMenuClickId: 1, ...route }));
};
</script>
<style scoped lang="scss"></style>
